from django.contrib import admin
from django.utils.translation import gettext_lazy as _
from . import models

from lib.django.admin import ImportExportFieldUseVerboseNameMixin, ModelAdmin
from unfold.contrib.import_export.forms import ImportForm, SelectableFieldsExportForm
from import_export.admin import ImportExportModelAdmin
from import_export import resources

# Resources  ============================================

class ProjectResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.Project

class PlanResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.Plan

class InitiationDataResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.InitiationData

class EthicalReviewResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.EthicalReview

class BeforeImplementResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.BeforeImplement

class HumanGeneticResourceResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.HumanGeneticResource

class HGRInternationalCooperationResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.HGRInternationalCooperation

class HGRCollectionResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.HGRCollection

class HGRStorageResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.HGRStorage

class HGRMaterialExportResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.HGRMaterialExport

class HGRDataFilingResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.HGRDataFiling

class ContractResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.Contract

class MaterialResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.Material

class BeginningResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.Beginning

class ImplementResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.Implement

class QualityControlResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.QualityControl

class FinishResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.Finish

class ExaminationResources(ImportExportFieldUseVerboseNameMixin, resources.ModelResource):
    class Meta:
        model = models.Examination

# Admins  ============================================

@admin.register(models.Project)
class ProjectAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [ProjectResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    

    permission_required = ["user.can_manage_projects"]

    list_display = ['id', 'chinese_name', 'english_name', 'drug_name', 'device_name', 
                    'drug_category', 'creator', 'is_innovative_drug_device',
                    'is_domestic_multicenter', 'is_epidemic_control', 'is_imported_registration',
                    'is_international_multicenter', 'is_lead_center']
    list_display_links = ['id', 'chinese_name']
    list_select_related = ('user',)
    search_fields = ['chinese_name', 'english_name', 'drug_name', 'device_name']
    list_filter = ['drug_category', 'is_innovative_drug_device',
                   'is_domestic_multicenter']


@admin.register(models.Plan)
class PlanAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [PlanResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]
    
    list_display = ['id', 'project', 'time', 'title', 'is_draft']
    list_display_links = ['id', 'title']
    list_select_related = ['project']
    search_fields = ['title', 'content']
    list_filter = ['is_draft']

@admin.register(models.InitiationData)
class InitiationDataAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [InitiationDataResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]
    
    list_display = ['id', 'project', 'time', 'version', 'is_draft']
    list_display_links = ['id']
    list_select_related = ['project']
    list_filter = ['is_draft']

@admin.register(models.EthicalReview)
class EthicalReviewAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [EthicalReviewResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]
    
    list_display = ['id', 'project', 'time', 'approval_time', 'approval_projcet_time']
    list_display_links = ['id']
    list_select_related = ['project']
    search_fields = ['note']

@admin.register(models.BeforeImplement)
class BeforeImplementAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [BeforeImplementResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]
    
    list_display = ['id', 'project', 'is_materials_complete', 'is_confirmed_by_manager']
    list_display_links = ['id']
    list_select_related = ['project']
    list_filter = ['is_materials_complete', 'is_confirmed_by_manager']

@admin.register(
    models.HumanGeneticResource,
)
class HumanGeneticResourceAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [HumanGeneticResourceResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]

    list_display = ['id', 'project', 'is_team_leader', 'is_partner_unit']
    list_display_links = ['id']
    list_select_related = ['project']
    list_filter = ['is_team_leader', 'is_partner_unit']

@admin.register(
    models.HGRInternationalCooperation,
    models.HGRCollection,
    models.HGRStorage,
    models.HGRMaterialExport,
    models.HGRDataFiling,
)
class HGRChildAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [HGRInternationalCooperationResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]

    list_display = ['id', 'HGR', 'approval_time']
    list_display_links = ['id']
    list_select_related = ['HGR__project']
    list_filter = ['approval_time']

@admin.register(models.Contract)
class ContractAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [ContractResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]

    list_display = ['id', 'project', 'contract_name', 'contract_time', 'contract_amount']
    list_display_links = ['id', 'contract_name']
    list_select_related = ['project']
    search_fields = ['contract_name']

@admin.register(models.Material)
class MaterialAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [MaterialResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]

    list_display = ['id', 'project', 'name', 'quantity', 'received_date', 'requires_return']
    list_display_links = ['id', 'name']
    list_select_related = ['project']
    search_fields = ['name']
    list_filter = ['requires_return']

@admin.register(models.Beginning)
class BeginningAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [BeginningResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]

    list_display = ['id', 'project', 'date_of_national_kickoff', 'date_of_center_kickoff']
    list_display_links = ['id']
    list_select_related = ['project']

@admin.register(models.Implement)
class ImplementAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [ImplementResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]

    list_display = ['id', 'project', 'number_of_enrolled_cases', 'design_num', 'actual_num']
    list_display_links = ['id']
    list_select_related = ['project']

@admin.register(models.QualityControl)
class QualityControlAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [QualityControlResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]

    list_display = ['id', 'project', 'is_confirmed', 'date_of_inspection']
    list_display_links = ['id']
    list_select_related = ['project']
    list_filter = ['is_confirmed']

@admin.register(models.Finish)
class FinishAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [FinishResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]

    list_display = ['id', 'project', 'quality_control_time', 'archive_time']
    list_display_links = ['id']
    list_select_related = ['project']

@admin.register(models.Examination)
class ExaminationAdmin(ModelAdmin, ImportExportModelAdmin):
    resource_classes = [ExaminationResources]
    import_form_class = ImportForm
    export_form_class = SelectableFieldsExportForm
    permission_required = ["user.can_manage_projects"]

    list_display = ['id', 'project', 'investigation_time', 'unit_personnel']
    list_display_links = ['id']
    list_select_related = ['project']
